package com.wzxy.medical_backed.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzxy.medical_backed.entity.Payment;
import com.wzxy.medical_backed.service.PaymentService;
import com.wzxy.medical_backed.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/payments")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @GetMapping("/getall")
    public Result getAll(@RequestParam(required = false) String keyword,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer size) {
        QueryWrapper<Payment> queryWrapper = new QueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) {
            queryWrapper.like("insuredName", keyword)
                    .or().like("cardId", keyword);
        }
        Page<Payment> paymentPage = paymentService.page(new Page<>(page, size), queryWrapper);
        return new Result("查询成功", paymentPage, 200);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Payment payment) {
        payment.setPaymentDate(new Date()); // 设置当前时间为缴费日期
        return new Result("添加成功", paymentService.save(payment), 200);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Payment payment) {
        return new Result("更新成功", paymentService.updateById(payment), 200);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return new Result("删除成功", paymentService.removeById(id), 200);
    }
}